package com.cqgc.controller;

import com.cqgc.domain.Harvest;
import com.cqgc.domain.PageHelper;
import com.cqgc.domain.Result;
import com.cqgc.service.HarvestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("harvest")
public class HarvestController {
    @Autowired
    private HarvestService harvestService;
    Result result = new Result();

    @GetMapping("list")
    public Result findAll(PageHelper pageHelper) {
        if (pageHelper != null) {
            int page = (pageHelper.getCurrentPage() - 1) * pageHelper.getPageSize();
            pageHelper.setCurrentPage(page);
        }
        List<Harvest> all = harvestService.findAll(pageHelper);
        List<Harvest> count = harvestService.findAllHarvest();
        if (all != null) {
            result.setCode(1);
            result.setMsg("查询成功");
            result.setData1(all);
            result.setTotal(count.size());
        } else {
            result.setCode(0);
            result.setMsg("查询失败");
        }
        return result;
    }

    @GetMapping("look")
    public Result Search(int hid){
       Harvest HarvestByHid = harvestService.findHarvestByHid(hid);
       if (HarvestByHid != null) {
           result.setCode(1);
           result.setMsg("查找成功");
       }else {
           result.setCode(0);
           result.setMsg("查找失败");
       }
       return result;
    }
    @PostMapping("edit")
    public Result editHarvest(@RequestBody Harvest harvest) {
       Harvest HarvestByHid = harvestService.findHarvestByHid(harvest.getHid());
        System.out.println(HarvestByHid.toString());
        if (harvest != null) {
            if (harvest.getHname() == null) {
                harvest.setHname(HarvestByHid.getHname());
            }
            if (harvest.getLanNumber() == null) {
                harvest.setLanNumber(HarvestByHid.getLanNumber());
            }
            if (harvest.getErNumber() == null) {
                harvest.setErNumber(HarvestByHid.getErNumber());
            }
            if (harvest.getYvjingNumber() == null) {
                harvest.setYvjingNumber(HarvestByHid.getYvjingNumber());
            }
            if (harvest.getOutlineNumber() == null) {
                harvest.setOutlineNumber(HarvestByHid.getOutlineNumber());
            }
        } else {
            result.setCode(0);
            result.setMsg("修改失败");
            return result;
        }

        int i = harvestService.editHarvest(harvest);

        if (i > 0) {
            System.out.println(harvest.getHname() + harvest.getErNumber());
            result.setCode(1);
            result.setMsg("修改成功");
        } else {
            result.setCode(0);
            result.setMsg("修改失败");
        }
        return result;
    }

    @PostMapping("add")
    public Result AddUser(@RequestBody Harvest harvest) {
//        if (harvest != null) {
//            user.setLastLoginTime(new Date());
//            String subString = user.getAccount().substring(5, 11);
//
//            user.setPassword(subString);
//            user.setRagTime(new Date());
//            user.setName(user.getAccount());
//
//        }

        int i = harvestService.addHarvest(harvest);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("注册成功");

        } else {
            result.setCode(0);
            result.setMsg("注册失败");
        }
        return result;
    }

    @PostMapping("del")
    public Result delHarvest(@RequestBody Harvest harvest) {
        int i = harvestService.deleteHarvest(harvest);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("删除成功");
        } else {
            result.setCode(0);
            result.setMsg("删除失败");
        }

        return result;
    }
    @PostMapping("ban")
    public Result banHarvest(@RequestBody Harvest harvest) {
        int i = harvestService.banHarvest(harvest);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("禁用成功");
        } else {
            result.setCode(0);
            result.setMsg("禁用失败");
        }

        return result;
    }
}
